from couchdb.client import Server
from django.conf import settings

def get_server_object():
    server_object = Server(getattr(settings, 'COUCHDB_SERVER', 'http://127.0.0.1:5984'))
    return server_object
    
def get_database_object():
    database_name = settings.COUCHDB_DATABASE_NAME
    server = get_server_object()
    if database_name in server:
        database_object = server[database_name]
    else:
        database_object = server.create(database_name)
    return database_object

def convert_database_to_dictionary(database):
    dictionary = {}
    for document_id in database:
        document = database[document_id]
        document_dictionary = convert_document_to_dictionary(document)
        dictionary[document_id] = document_dictionary
    return dictionary
    
def convert_document_to_dictionary(document):
    dictionary = {}
    for field in document:
        dictionary[field] = document[field]
    return dictionary

